package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.AuthSupport;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private DtsGoodsService dtsGoodsService;

    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="查找")
    @RequiresPermissions("admin:goods:list")
    @GetMapping("/list")
    public Object goodsList(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            @RequestParam(defaultValue = "add_time") String sort,
                            @RequestParam(defaultValue = "desc") String order){
        IPage<DtsGoods> iPage = dtsGoodsService.findPage(page,limit,sort,order);

        long total = iPage.getTotal();
        List<DtsGoods> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);

        return ResponseUtil.ok(map);
    }

    @Autowired
    private DtsCategoryService dtsCategoryService;

    @Autowired
    private DtsBrandService dtsBrandService;


    @GetMapping("/catAndBrand")
    public Object catAndBrand(){

        List<CatVo> categoryList =dtsCategoryService.findName();

        for (CatVo catVo : categoryList) {
            catVo.setChildren(dtsCategoryService.findName2(catVo.getValue())) ;
        }

        List<CatVo> catVoList = dtsBrandService.selectName();

        Map map = new HashMap();

        map.put("categoryList", categoryList);
        map.put("brandList", catVoList);

        return ResponseUtil.ok(map);
    }

    @GetMapping("/detail")
    public Object detail(Integer id){

       DtsGoods dtsGoods = dtsGoodsService.selectById(id);
       List<DtsGoodsSpecification> dtsGoodsSpecificationList = dtsGoodsSpecificationService.findByGoodsId(id);
       List<DtsGoodsProduct> dtsGoodsProductList = dtsGoodsProductService.findByGoodId(id);
       List<DtsGoodsAttribute> dtsGoodsAttributeList = dtsGoodsAttributeService.findByGoodId(id);
       String L2 = dtsGoodsService.selectCategoryId(id);
       String L1 = dtsCategoryService.selectByPid(L2);
       String[] strings = {L1,L2};

       Map map = new HashMap();
       map.put("goods", dtsGoods);
       map.put("specifications", dtsGoodsSpecificationList);
       map.put("products", dtsGoodsProductList);
       map.put("categoryIds", strings);
        return ResponseUtil.ok(map);
    }
    @Autowired
    private DtsGoodsAttributeService dtsGoodsAttributeService;

    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;

    @Autowired
    private DtsGoodsSpecificationService dtsGoodsSpecificationService;

    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="添加")
    @RequiresPermissions("admin:goods:create")
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){

        dtsGoodsService.insert(goodsAllinone.getGoods());
        dtsGoodsAttributeService.insert(goodsAllinone.getAttributes());
        dtsGoodsProductService.insertProducts(goodsAllinone.getProducts());
        dtsGoodsSpecificationService.insert(goodsAllinone.getSpecifications());
        return ResponseUtil.ok();

    }
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="删除")
    @RequiresPermissions("admin:goods:delete")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods dtsGoods){
        dtsGoodsService.delete(dtsGoods);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button ="编辑")
    @RequiresPermissions("admin:goods:update")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        DtsGoods goods = goodsAllinone.getGoods();
        Integer gid = goods.getId();
        dtsGoodsService.update(goods);
        dtsGoodsAttributeService.deleteByGid(gid);
        dtsGoodsProductService.deleteByGid(gid);
        dtsGoodsSpecificationService.deleteByGid(gid);

        dtsGoodsProductService.insertProducts(goodsAllinone.getProducts());
        dtsGoodsProductService.insertProducts(goodsAllinone.getProducts());
        dtsGoodsSpecificationService.insert(goodsAllinone.getSpecifications());

        return ResponseUtil.ok();
    }


}
